/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkStateGridA/MkStateGridA.h"
#include "MkUtil/MkLog.h"
#include "MkEvent/MkEventPool.h"
#include "MkEvent/MkTimer.h"
#include <signal.h>

Uint32 MessageReqestCallback(const CMkStateGridARequestBase* pRequest, const MkStateGridAResponseCallback& ResCb)
{
    switch (pRequest->GetMsgType()) {
    case MkStateGridASubscribeNotify:
        MkDebugLog("recv alarm notify\n");
        ResCb(MkSipStatusOK, nullptr);
        break;
    case MkStateGridAMediaShutDown:
        MkDebugLog("recv remote media shut down\n");
        ResCb(MkSipStatusOK, nullptr);
        break;
    default:
        break;
    }
    return NoneError;
}

MkString AlarmSubscribeId = "111";
MkString StatusSubscribeId = "222";
MkString VideoPhoneSession = "555";
MkString RealplaySession = "666";
MkString BroadcastSession = "777";
CMkTimer     g_Timer;
Uint16 LowSipPort = 5070;
MkString LowSipId = "34020000002000000001";
MkString LowSipRealm = "3402000000";
MkString CurSipId = "34030000003000000001";
MkString CurSipReaml = "3403000000";
MkString LowSipHost = "192.168.0.24";
int main(int argc, char ** argv)
{

#ifdef _WIN32
    WSADATA WSAData;
    WSAStartup(0x101, &WSAData);
#else
    signal(SIGPIPE, SIG_IGN);  //ignore sigpipe
#endif
    MkLogChangeLevel(LOG_DEBUG);
    CMkEventPool Pool;

    Pool.Start();
    g_Timer.StartTimerThread();
    CMkStateGridA UpSvr(&Pool, &g_Timer);
    UpSvr.Start(CurSipId, CurSipReaml, "", 5080, [](const CMkStateGridARequestBase* pRequest, const MkStateGridAResponseCallback& ResCb) {
        return MessageReqestCallback(pRequest, ResCb);
    });

    signal(SIGINT, [](int sig) {
        if (SIGINT == sig) {
            g_Timer.LoopBreak();
        }
    });// set exit  signal

    MkMap<MkString, Uint32> MapSubId;
    MkVectorString VecSubId;

    try {
        Uint32 tmp = std::stoi("aaa");
    } catch (const std::exception& ex) {
        printf("%s\n", ex.what());
    }


    MapSubId["111"] = 111;
    MapSubId["222"] = 222;
    VecSubId.push_back("12");
    VecSubId.push_back("23");
    char GetC;
    while (GetC = getchar()) {
        BOOL bQuit = FALSE;
        MkDebugLog("get char:%c\n", GetC);
        switch (GetC) {
        case '1':
            UpSvr.RealPlay(LowSipId, "34020000001110000001", FALSE, LowSipRealm, LowSipHost, LowSipPort, "192.168.0.111", 111, [](Uint32 ErrorCode, const CMkStateGridAResponseBase* pResponse) {
                MkDebugLog("real play return error code:%d\n", ErrorCode);
                const  CMkStateGridAMediaResponse *pRes = dynamic_cast<const CMkStateGridAMediaResponse*>(pResponse);
                if (pRes && MkSipStatusOK == ErrorCode) {
                    RealplaySession = pRes->GetSessionId();
                }

            });
            break;
        case '2':
            //UpSvr.Broadcast(LowSipId,"34020000001210000001",LowSipRealm, LowSipHost, LowSipPort, "192.168.0.111", 111, [](Uint32 ErrorCode, const CMkStateGridAResponseBase* pResponse) {
            //    MkDebugLog("broadcast return error code:%d\n", ErrorCode);
            //    const  CMkStateGridAMediaResponse *pRes = dynamic_cast<const CMkStateGridAMediaResponse*>(pResponse);
            //    BroadcastSession = pRes->GetSessionId();
            //});
            break;
        case '3':
            //UpSvr.VideoPhone(LowSipId,"34020000001310000001",LowSipRealm,LowSipHost, LowSipPort, "192.168.0.111", 111,222, [](Uint32 ErrorCode, const CMkStateGridAResponseBase* pResponse) {
            //    MkDebugLog("video phone return error code:%d\n", ErrorCode);
            //    const  CMkStateGridAMediaResponse *pRes = dynamic_cast<const CMkStateGridAMediaResponse*>(pResponse);
            //    VideoPhoneSession = pRes->GetSessionId();
            //});
            break;
        case '4':
            UpSvr.PtzControl(LowSipId, "34020000001110000001", LowSipRealm, LowSipHost, LowSipPort, 0x0101, 1, 2, 3, [](Uint32 ErrorCode, const CMkStateGridAResponseBase* pResponse) {
                MkDebugLog("query flow return error code:%d\n", ErrorCode);
            });
            break;
        case '5':
            UpSvr.Ptz3DZoom(LowSipId, "34020000001110000001", LowSipRealm, LowSipHost, LowSipPort, 0x0001, 1, 2, 3, 4, 5, 6, 7, [](Uint32 ErrorCode, const CMkStateGridAResponseBase* pResponse) {
                MkDebugLog("query flow return error code:%d\n", ErrorCode);
            });
            break;
        case '6':
            UpSvr.AlarmSubscribe(LowSipId, "34020000001110000001", LowSipRealm, LowSipHost, LowSipPort, 3600, MapSubId, [](Uint32 ErrorCode, const CMkStateGridAResponseBase* pResponse) {
                MkDebugLog("alarm subscribe return error code:%d\n", ErrorCode);
            });
            break;
        case '7':
            UpSvr.StatusSubscribe(LowSipId, "34020000001110000001", LowSipRealm, LowSipHost, LowSipPort, 3600, VecSubId, [](Uint32 ErrorCode, const CMkStateGridAResponseBase* pResponse) {
                MkDebugLog("status subscribe return error code:%d\n", ErrorCode);
            });
            break;
        case '8':
            //UpSvr.StopVideoPhone(VideoPhoneSession, [](Uint32 ErrorCode, const CMkStateGridAResponseBase* pResponse) {
            //    MkDebugLog("query flow return error code:%d\n", ErrorCode);
            //});
            break;
        case '9':
            UpSvr.StopRealPlay(RealplaySession, [](Uint32 ErrorCode, const CMkStateGridAResponseBase* pResponse) {
                MkDebugLog("query flow return error code:%d\n", ErrorCode);
            });
            break;
        case 'a':
            //UpSvr.StopBrodcast(BroadcastSession, [](Uint32 ErrorCode, const CMkStateGridAResponseBase* pResponse)
            //{
            //    MkDebugLog("query flow return error code:%d\n", ErrorCode);
            //});
            break;
        case 'b':
            UpSvr.StopAlarmSubscirbe(AlarmSubscribeId, [](Uint32 ErrorCode, const CMkStateGridAResponseBase* pResponse) {
                MkDebugLog("query flow return error code:%d\n", ErrorCode);
            });
            break;
        case 'c':
            UpSvr.StopStatusSubscirbe(StatusSubscribeId, [](Uint32 ErrorCode, const CMkStateGridAResponseBase* pResponse) {
                MkDebugLog("query flow return error code:%d\n", ErrorCode);
            });
        case 'q':
            bQuit = TRUE;
            break;
        default:
            break;
        }
        if (bQuit) {
            break;
        }
    }
    g_Timer.StopTimerThread();
    Pool.Stop();
    UpSvr.Stop();
#ifdef _WIN32
    WSACleanup();
#endif
    printf("end\n");
    return 0;
}


